﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace GenBenEditor
{
    public partial class TilePalette : UserControl
    {
        public Map map;

        public int selected_tileset;
        public TileId left_tile;
        public TileId right_tile;

        private Brush alpha_background;

        public TilePalette()
        {
            InitializeComponent();

            alpha_background = new HatchBrush(HatchStyle.LargeCheckerBoard, Color.Black);

            selected_tileset = 0;
            left_tile = new TileId(0, 0);
            right_tile = new TileId(0, 1);

            this.tilesetBox.MouseClick += new MouseEventHandler(tilesetBox_MouseClick);
            this.tilesetBox.Paint += new PaintEventHandler(tilesetBox_Paint);
            this.leftBox.Paint += new PaintEventHandler(leftBox_Paint);
            this.rightBox.Paint += new PaintEventHandler(rightBox_Paint);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            tilesetBox.Invalidate();
            leftBox.Invalidate();
            rightBox.Invalidate();

            base.OnPaint(e);
        }

        private void prevButton_Click(object sender, EventArgs e)
        {
            if (map == null)
                return;

            if (selected_tileset > 0)
            {
                selected_tileset--;
                tilesetCounter.Text = String.Format("{0} / {1}", selected_tileset+1, map.tilesets.Count);
                tilesetBox.Invalidate();
            }
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            if (map == null)
                return;

            if (selected_tileset < map.tilesets.Count - 1)
            {
                selected_tileset++;
                tilesetCounter.Text = String.Format("{0} / {1}", selected_tileset+1, map.tilesets.Count);
                tilesetBox.Invalidate();
            }
        }

        private void tilesetBox_Paint(object sender, PaintEventArgs e)
        {
            if (map == null)
                return;

            if (map.tilesets.Count <= selected_tileset)
                return;

            PictureBox pbox = (PictureBox)sender;
            e.Graphics.FillRectangle(Brushes.White, pbox.ClientRectangle);
            e.Graphics.FillRectangle(alpha_background, pbox.ClientRectangle);
            e.Graphics.DrawImage(map.tilesets[selected_tileset].bitmap, new Point(0, 0));

            e.Graphics.DrawLine(Pens.DarkRed, new Point(0, 240), new Point(15, 255));
            e.Graphics.DrawLine(Pens.DarkRed, new Point(15, 240), new Point(0, 255));
            
            if (map.tilesets.Count <= left_tile.tileset || map.tilesets.Count <= right_tile.tileset)
                return;

            Rectangle left_mouse_rect = map.tilesets[left_tile.tileset].GetTileRect(left_tile.tile);
            Rectangle right_mouse_rect = map.tilesets[right_tile.tileset].GetTileRect(right_tile.tile);

            left_mouse_rect.Width -= 1;
            left_mouse_rect.Height -= 1;
            right_mouse_rect.Width -= 1;
            right_mouse_rect.Height -= 1;

            if(selected_tileset == left_tile.tileset)
                e.Graphics.DrawRectangle(Pens.Blue, left_mouse_rect);
            if (selected_tileset == right_tile.tileset)
                e.Graphics.DrawRectangle(Pens.Red, right_mouse_rect);
        }

        private void leftBox_Paint(object sender, PaintEventArgs e)
        {
            if (map == null)
                return;

            if (map.tilesets.Count <= left_tile.tileset)
                return;

            PictureBox pbox = (PictureBox)sender;
            e.Graphics.FillRectangle(Brushes.White, pbox.ClientRectangle);
            e.Graphics.FillRectangle(alpha_background, pbox.ClientRectangle);
            e.Graphics.DrawImage(map.tilesets[left_tile.tileset].bitmap,
                pbox.ClientRectangle, map.tilesets[left_tile.tileset].GetTileRect(left_tile.tile), GraphicsUnit.Pixel);
        }

        private void rightBox_Paint(object sender, PaintEventArgs e)
        {
            if (map == null)
                return;

            if (map.tilesets.Count <= right_tile.tileset)
                return;

            PictureBox pbox = (PictureBox)sender;
            e.Graphics.FillRectangle(Brushes.White, pbox.ClientRectangle);
            e.Graphics.FillRectangle(alpha_background, pbox.ClientRectangle);
            e.Graphics.DrawImage(map.tilesets[right_tile.tileset].bitmap,
                pbox.ClientRectangle, map.tilesets[right_tile.tileset].GetTileRect(right_tile.tile), GraphicsUnit.Pixel);
        }

        private void tilesetBox_MouseClick(object sender, MouseEventArgs e)
        {
            if (map == null)
                return;

            if (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right)
            {
                int tile_x = (int)(e.X / Tileset.tile_width);
                int tile_y = (int)(e.Y / Tileset.tile_height);

                TileId new_tile = new TileId((byte)selected_tileset, 
                    (byte)(tile_y * (Tileset.width / Tileset.tile_width) + tile_x));

                if (e.Button == MouseButtons.Left)
                {
                    left_tile = new_tile;
                    tilesetBox.Invalidate();
                    leftBox.Invalidate();
                }
                else
                {
                    right_tile = new_tile;
                    tilesetBox.Invalidate();
                    rightBox.Invalidate();
                }
            }
        }
    }
}
